﻿using Microsoft.Build.Evaluation;
using NuGet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OD.ProjectCustomizationTool.Helpers
{
    internal static class IPackageMetadataExtensions
    {
        /// <summary>
        /// Build the package installed folder name.
        /// </summary>
        /// <param name="packageMetadata">The considered package.</param>
        /// <returns>Returns the name of the package folder.</returns>
        public static String FolderName(this IPackageMetadata packageMetadata)
        {
            return packageMetadata.Id + "." + packageMetadata.Version.ToString();
        }

        /// <summary>
        /// Retrieve the path to a package folder under the solution path.
        /// </summary>
        /// <param name="packageMetadata">The considered package.</param>
        /// <returns>Returns the relative path from the solution folder to the package folder.</returns>
        public static String InnerSolutionPath(this IPackageMetadata packageMetadata)
        {
            return Path.Combine(Properties.Resources.NuGetPackageFolderName, packageMetadata.FolderName());
        }

        /// <summary>
        /// Retrieve the full path to the package folder.
        /// </summary>
        /// <param name="packageMetadata">The package entity.</param>
        /// <param name="project">The project to which the install has to be done.</param>
        /// <returns>Returns the full path to the package folder.</returns>
        public static String FullPath(this IPackageMetadata packageMetadata, Project project)
        {
            if (project == null)
            {   // This code has been generated from the 'argnull' custom snippet.
                throw new ArgumentNullException("project");
            }

            return Path.Combine(
                project.GetSolutionFullPath(),
                packageMetadata.InnerSolutionPath());
        }

    }
}
